package cn.topcodes.tcsf.admin.web.controller.impl;

import cn.topcode.unicorn.utils.ValidationUtil;
import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.admin.scheduling.ScheduleJob;
import cn.topcodes.tcsf.admin.scheduling.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

/**
 * @author Unicorn
 * @date 2017/4/20
 */
@Controller
@RequestMapping("/admin/schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @RequestMapping(value = "",method = RequestMethod.GET)
    public String index() {
        return "admin/schedule";
    }

    @ResponseBody
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ApiResponseEntity<List<ScheduleJob>> list() {
        ApiResponseEntity<List<ScheduleJob>> apiResponseEntity = ApiResponseEntity.buildOK();
        try {
            List<ScheduleJob> list = scheduleService.getAllJob();
            apiResponseEntity.setBody(list);
        } catch (Exception e) {
            e.printStackTrace();
            apiResponseEntity.setBody(Collections.emptyList());
        }
        return apiResponseEntity;
    }

    @RequestMapping(value = "create",method = RequestMethod.GET)
    public String create() {
        return "admin/schedule-add";
    }

    @RequestMapping(value = "update",method = RequestMethod.GET)
    public String update(@RequestParam String name, @RequestParam String group, Model model) {

        return "admin/schedule-modify";
    }

    @ResponseBody
    @RequestMapping(value = "create",method = RequestMethod.POST)
    public ApiResponseEntity<Void> create(@Valid ScheduleJob job, BindingResult result) {
        ValidationUtil.validate(result);
        try {
            this.scheduleService.addJob(job);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponseEntity.buildFailed(e.getMessage());
        }
        return ApiResponseEntity.buildOK();
    }

    @ResponseBody
    @RequestMapping(value = "pause",method = RequestMethod.POST)
    public ApiResponseEntity<Void> pause(@RequestParam String jobName, @RequestParam String jobGroup) {
        try {
            this.scheduleService.pauseJob(jobGroup, jobName);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponseEntity.buildFailed(e.getMessage());
        }
        return ApiResponseEntity.buildOK();
    }

    @ResponseBody
    @RequestMapping(value = "resume",method = RequestMethod.POST)
    public ApiResponseEntity<Void> resume(@RequestParam String jobName, @RequestParam String jobGroup) {
        try {
            this.scheduleService.resumeJob(jobGroup, jobName);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponseEntity.buildFailed(e.getMessage());
        }
        return ApiResponseEntity.buildOK();
    }

    @ResponseBody
    @RequestMapping(value = "run",method = RequestMethod.POST)
    public ApiResponseEntity<Void> run(@RequestParam String jobName, @RequestParam String jobGroup) {
        try {
            this.scheduleService.runAJobNow(jobGroup, jobName);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponseEntity.buildFailed(e.getMessage());
        }
        return ApiResponseEntity.buildOK();
    }

    @ResponseBody
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public ApiResponseEntity<Void> delete(@RequestParam String jobName, @RequestParam String jobGroup) {
        try {
            this.scheduleService.deleteJob(jobGroup, jobName);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponseEntity.buildFailed(e.getMessage());
        }
        return ApiResponseEntity.buildOK();
    }

}
